﻿<?php

    //优化初始化
    include('init.php');
	
    if(!empty($_POST)){
    	
		//从post中取出用户提交的数据 
    	$username = $_POST['username'];
		$password = md5($_POST['password']);
		
		//勾选时加密的密码出错时修改
        		if(!empty($_COOKIE['password']) && $_POST['password'] == $_COOKIE['password']){
        			$password = $_COOKIE['password'];
        		}
		
		
		
		//编写查询语句
		$sql = "select * from `admin` where `username`='$username' and `password`='$password'" ;
		
		
		//执行上面组装好的查询语句
		$result =mysqli_query($link,$sql);
		 
		 //该函数是将查询到的数据转换成数组格式
		$data = mysqli_fetch_assoc($result);
		
		//判断变量是否不为空
		if(!empty($data)){
			//开启session然后将用户信息存储到$_SESSION中
			//session_start();
			//将用户信息存储到$_SESSION的admin下标中
			$_SESSION['admin']=$data;
			
			
			//记住密码
			//（$_POST['check'])下标不为空表示用户勾选了记住密码，那么就要把用户名和密码储存到cookie中
			if(!empty($_POST['check'])){
			 
				//储存帐号密码一天时间写法
				setcookie('username',$data['username'],time()+3600*24);
				setcookie('password',$data['password'],time()+3600*24);
			}else{
				//echo 88;die;
				//用户取消记住我清除cookie信息
				setcookie('username','',0);
				setcookie('password','',0);
			}


            //判断用户是否勾选自动登录，如果自动登录勾选了就将它储存在cookie中
            if(!empty($_POST['auto'])){
            	setcookie('auto_login',1,time()+3600*24);
            }else{
            	setcookie('auto_login',0,0);
            }
			
			
			//JS跳转语句登录
			// echo '<script>  /*alert("登陆成功");*/ location.href="index.php";</script>';die;
		    //php跳转语句登录
		     header('location:index.php');
		}else{
			echo'<script> alert("登录失败");history.go(-1);</script>';die;
		}
    }
    
    
    //加载登录页视图
    include ('view/login.html');

?>
